package com.carlife.provider;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.carlife.algorithm.Fitting;
import com.carlife.algorithm.FittingFactory;
import com.carlife.model.MileageLog;
import com.carlife.model.OilLog;
import com.carlife.ui.MessageAgent;
import com.carlife.util.Constants;
import com.carlife.util.DateUtil;

import android.content.Context;
import android.test.mock.MockApplication;
import android.util.Log;

/**
 * 根据加油履历和路程变更履历，计算油耗数据
 * @author luyuan
 *
 */
public class CarStatusMgr extends DBEngineer {

	public CarStatusMgr(Context ctx) {
		super(ctx);
	}
	
	/**
	 * 取得最近更新的里程记录
	 * @return
	 */
	public MileageLog getCrntMileage() {
		MileageLog model = new MileageLog();
		Conditions cond = new Conditions();
		cond.setLimit(1);
		cond.addOrder(MileageLog.FD_INPUT_TIME, false);
		
		List<IModel> modelList = query(model, cond);
		if (modelList.size() == 0) {
			MessageAgent.publishMessage("目前还没有任务里程信息！");
			model.put(MileageLog.FD_INPUT_TIME, DateUtil.currentTimeFormatted());
			model.put(MileageLog.FD_MILEAGE_VAlUE, "0");
		}
		
		return (MileageLog)modelList.get(0);
	}
	
	/**
	 * 取得指定日期的里程（估算值）
	 * @param date
	 * @return
	 */
	public BigDecimal getMileage(String date) {
		MileageLog model = new MileageLog();
		Conditions cond = new Conditions();
		cond.addOrder(MileageLog.FD_INPUT_TIME, true);
		List<IModel> modelList = query(model, cond);
		if (modelList.size() == 0) {
			MessageAgent.publishMessage("目前还没有任务里程信息！");
			return new BigDecimal(0);
		}

		int size = modelList.size();
		List newModelList = new ArrayList(size);
		String tmpDateStr = null;
		for (int i=0; i < size; i++) {
			MileageLog log = (MileageLog)modelList.get(size-i-1);
			if (tmpDateStr == null) {
				tmpDateStr = log.get(log.FD_INPUT_TIME).toString();
			} else {
				double differece = DateUtil.getIncreament(tmpDateStr, log.get(log.FD_INPUT_TIME).toString());
				if (differece == 0) {
					continue;
				}
			}
			newModelList.add(log);
		}
		
		size = newModelList.size();
		double [] point_x = new double[size];
		double [] point_y = new double[size];
		
		String baseDateStr = null;
		
		
		for (int i = 0; i < size; i++) {
			MileageLog log = (MileageLog)newModelList.get(size-i-1);
			point_y[i] = new BigDecimal(log.get(log.FD_MILEAGE_VAlUE).toString()).doubleValue();
			
			if (i == 0) {
				baseDateStr = log.get(log.FD_INPUT_TIME).toString();
				point_x[i] = 0;
			} else {
				point_x[i] = DateUtil.getIncreament(baseDateStr, log.get(log.FD_INPUT_TIME).toString());
			}
		}
		
		Fitting fitting = FittingFactory.getDefaultFitting();
		fitting.initial(point_x, point_y);
		
		return new BigDecimal(fitting.fittingY(DateUtil.getIncreament(baseDateStr, date)));
	}
	
	/**
	 * 取得到目前为止的加油费用总和
	 * @return
	 */
	public BigDecimal getCrntOilCost() {
		BigDecimal result = new BigDecimal(0);
		OilLog model = new OilLog();
		Conditions cond = new Conditions();
		cond.addOrder(OilLog.FD_INPUT_TIME, true);
		
		List modelList = query(model, cond);
		Iterator ite = modelList.iterator();
		while (ite.hasNext()) {
			OilLog log = (OilLog)ite.next();
			result.add(new BigDecimal(log.get(log.FD_OIL_MONEY).toString()));
		}
		result.setScale(Constants.SCALE_MONEY);
		return result;
	}
	
	/**
	 * 到某个时间点为止消耗的油钱
	 * @param date
	 * @return
	 */
	public BigDecimal getOilCost(String date) {
		// TODO
		return null;
	}
	
	/**
	 * 某个时间段内消耗的油钱
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public BigDecimal getOilCost(String startDate, String endDate) {
		// TODO
		return null;
	}
	
	

}
